<template>
    <div class="BoxFingerprintDrawer">
        <el-drawer
            :title="drawer.tit"
            :visible.sync="drawer.open"
            :direction="drawer.way">
            <el-form :model="dataForm" ref="dataForm" label-width="180px" :rules="roleRules">
                  <el-form-item label="系统用户" prop="userId">
                      <el-select v-model="dataForm.userId" clearable filterable placeholder="系统用户请选择对应用户名">
                        <el-option
                        v-for="item in pickUserList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                        ></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="姓 名" prop="userName" style="width: 70%;">
                    <el-input v-model="dataForm.userName" placeholder="非系统用户请输入姓名"></el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-button plain type="primary" @click="sureClick">保 存</el-button>
                    <el-button plain @click="returnList">取 消</el-button>
                  </el-form-item>
            </el-form>
        </el-drawer>
    </div>
</template>

<script>
    // 用户指纹列表抽屉 用于新增/修改列表数据
    import AuthApi from "@/api/system/auth";
    import EquipApi from '@/api/equip/equip';
    import * as message from "@/constants/message";

export default {
    name:'BoxFingerprintDrawer',
    data(){
        return{
            drawer:{
                tit:'',
                open: false, //默认隐藏
                way: 'rtl'  // 打开方向 rtl右到左 |ltr 左到右|ttb 上到下 |btt 下到上
            },
            dataForm:{
                boxId: '',
                userId: '',
                boxUserId: '',
                userName: ''
            },
            pickUserList: [],
            roleRules:{}

        }
    },
    created(){
        this.init();
    },
    methods:{
        init(){
            this.getUserList();
        },
        getUserList(){
            // 获取所有用户信息
            AuthApi.listAllAdmin()
                .then(res => {
                setTimeout(() => {
                this.pickUserList = res.data.map(item => {
                    return {
                        value: item.userId,
                        label: item.userName
                    }
                })
                }, 200)
            })
        },
        editData(){
            // 编辑
            var cond = {
                boxId: this.dataForm.boxId,
                userName: this.dataForm.userName,
                userId: this.dataForm.userId,
                boxUserId: this.dataForm.boxUserId,
            }
            EquipApi.editEquipBoxUser(cond)
                .then(res =>{
                    this.$Utils.notifySuccess();
                    this.returnList();
                })
        },
        addData(){
            var cond = {
                boxId: this.dataForm.boxId,
                userName: this.dataForm.userName,
                userId: this.dataForm.userId,
            }
            EquipApi.addEquipBoxUser(cond)
                .then(res =>{
                    this.$Utils.notifySuccess();
                    this.returnList();
                })
        },
        sureClick(){
            this.$confirm(message.MSG_CONFIRM_SAVE_ALTER, message.TITLE_PROMPT, {})
                .then(() => {
                    if(this.dataForm.boxUserId){
                        this.editData();
                    }else{
                        this.addData();
                    }
            });
        },
        returnList(){
            this.dataForm.boxId = '';
            this.dataForm.userId = '';
            this.dataForm.userName = '';
            this.dataForm.boxUserId = '';
            this.$parent.reload();
            this.drawer.open = false;
        },

        handleCreate(val){
            // 新增
            this.drawer.tit = '新增标本箱用户';
            this.drawer.open = true;
            this.dataForm.boxId = val
        },
        handleChange(row){
            this.drawer.tit = '编辑标本箱用户';
            this.drawer.open = true;
            this.dataForm.boxId = row.boxId
            this.dataForm.userName = row.userName
            this.dataForm.userId = row.userId
            this.dataForm.boxUserId = row.boxUserId
        }
    },

}
</script>

<style>

</style>
